<import resource="/Company Home/Data Dictionary/Scripts/json2.js">

var lat = parseFloat(args.lat);
var lon = parseFloat(args.lon);
var zoom = parseFloat(args.zoom);

if (zoom == null) zoom = 1.5 * .05;


if (lat == null || lon == null) {
	status.code = 400;
	status.message = "Error";
	status.redirect = true;
} else {
	var query = "";
	var left = lat - zoom;
	var right = lat + zoom;
	var top = lon + zoom;
	var bottom = lon - zoom;

	// NOTE: This should do a range query, but they don't work with negatives!!
	query = "+ASPECT:\"caas:locatable\"";
	logger.log("Doing "+query);
	var nodes = search.luceneSearch(query);
	
	var results = [];
	for (var i = 0; i < nodes.length; i++) {
		var node = nodes[i];
		lat = node.properties['caas:latitude'];
		lon = node.properties['caas:longitude'];
		if (left < lat && lat < right && bottom < lon && lon < top) {
			var id = String(node.id);
			var item = {
				id: id,
				name: node.properties['cm:name'],
				title: node.properties['cm:title'],
				description: node.properties['cm:description'],
				tags: node.properties['caas:tags'],
				lat: lat,
				lon: lon
			};
			
			results[results.length] = item;
		}
	}
	
	var jsonString = JSON.stringify(results);
	
	model.callback = args.callback;
	if (args.context == null) args.context = "null";
	model.context = args.context;
	model.output = jsonString;
	model.leftLine = left;
	model.rightLine = right;
	model.topLine = top;
	model.bottomLine = bottom;
}
